package cn.itsy.web.controller;

import cn.itsy.dao.CompanyinfoDao;
import cn.itsy.pojo.Companyinfo;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 添加数据Controller类
 */

@Controller
@RequestMapping("XiaoJianCompanyinfoController")
public class XiaoJianInsertController {
    @Resource
    private CompanyinfoDao cominfoimpl;

    /**
     * 单位信息设置
     * 添加信息至Companyinfo表
     * 请求路径insertCompanyinfo
     */
    @RequestMapping("/insertCompanyinfo")
    @ResponseBody
    public Map<String, Object> insertCompanyinfo(@RequestParam Map<String, Object> map) {
        System.out.println("添加方法！");
        System.out.println(map.toString());
        //判断是否有已有单位信息
        String ifsql = "select * from companyinfo";
        List<Companyinfo> listcompanyinfos = cominfoimpl.selectCompanyinfoDao(ifsql);
        int i = 0;
        if (listcompanyinfos.isEmpty()) {
            String sql = "insert into companyinfo values (default, '" + map.get("cname") + "', '" + map.get("clowyer") + "', '" + map.get("cleader") + "', '" + map.get("ctype") + "', '" + map.get("jitype") + "', '" + map.get("caddress") + "', '" + map.get("cbian") + "', '" + map.get("curl") + "', '" + map.get("cemail") + "', '" + map.get("ctel") + "', '" + map.get("cfax") + "', '" + map.get("createtime") + "', '" + map.get("cinfo") + "')";
            System.out.println(sql);
            i = cominfoimpl.insertCompanyinfoDao(sql);
        } else {
            i = 0;
            map.put("txt", "已经有一个单位信息了请修改或删除已有单位信息！");
        }
        map.put("i", i);

        return map;
    }

    /**
     * 单位信息修改查询
     * 添加信息至Companyinfo表
     * 请求路径insertCompanyinfo
     */
    @RequestMapping("/selectCompanyinfo")
    @ResponseBody
    public List<Companyinfo> selectCompanyinfo(@RequestParam Map<String, Object> map) {
        System.out.println("查询方法！");
        System.out.println(map.toString() + "-=-=-=======-=-=-=-=-=-=-=-=-=-=");
        String sql = "select * from companyinfo ";
        List<Companyinfo> companyinfos = cominfoimpl.selectCompanyinfoDao(sql);
        System.out.println(companyinfos.toString() + "-=-=-=-=-=-=-=-=-=-=-=");
        return companyinfos;
    }

    /**
     * 单位信息修改
     * 添加信息至Companyinfo表
     * 请求路径insertCompanyinfo
     */
    @RequestMapping("/updateCompanyinfo")
    public String updateCompanyinfo(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        System.out.println("去修改页面！");
        System.out.println(map.toString() + "-=-=-======" + map.get("id"));
        String sqlectsql = "select * from companyinfo where id = '" + map.get("id") + "' ";
        List<Companyinfo> companyinfos = cominfoimpl.selectCompanyinfoDao(sqlectsql);
        System.out.println(companyinfos + "====================");
        request.setAttribute("companyinfos", companyinfos);
        return "/danweiupdate";
    }

    /**
     * 单位信息修改正！
     * 添加信息至Companyinfo表
     * 请求路径insertCompanyinfo
     */
    @RequestMapping("/updateCompanyinfos")
    @ResponseBody
    public Map<String, Object> updateCompanyinfos(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        String updatesql = "update companyinfo set cname='" + map.get("id") + "',cname='" + map.get("cname") + "',clowyer='" + map.get("clowyer") + "',cleader='" + map.get("cleader") + "',ctype='" + map.get("ctype") + "',jitype='" + map.get("jitype") + "',caddress='" + map.get("caddress") + "',cbian='" + map.get("cbian") + "',curl='" + map.get("curl") + "',cemail='" + map.get("cemail") + "',ctel='" + map.get("ctel") + "',cfax='" + map.get("cfax") + "',cinfo='" + map.get("cinfo") + "' ";
        int i = cominfoimpl.insertCompanyinfoDao(updatesql);
        map.put("i", i);
        return map;
    }

    /**
     * 单位信息删除
     * 添加信息至Companyinfo表
     * 请求路径insertCompanyinfo
     */
    @RequestMapping("/deleteCompanyinfo")
    public String deleteCompanyinfo(@RequestParam String id, HttpServletRequest request) {
        System.out.println("删除方法！");
        System.out.println(id + "-=-=-======");
        String sql = "delete  from companyinfo where id='" + id + "' ";
        int i = cominfoimpl.insertCompanyinfoDao(sql);
        System.out.println(i + "=-=-=-=-=-=-=-=-=-=-=-");
        return "/updatecompanyinfo";
    }


}
